//
//  CityViewController.m
//  Lovelife
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CityViewController.h"
#import "SceneryViewController.h"
#import "AreaHeadReusableView.h"
#import "AreaCell.h"
#import "AFNetWorkingYU.h"
#import "UIImageView+WebCache.h"
#import "SceneryModel.h"
#import "CityModel.h"
#import "WebViewController.h"
#import "RecommendTripViewController.h"
#import "NightManager.h"
#import "MJRefresh.h"
@interface CityViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,AreaHeadReusableViewDelegate>
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicatorView;
@property (nonatomic, retain) NSMutableArray *arrayOfScenery;
@property (nonatomic, retain) AreaHeadReusableView *headView;
@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) CityModel *city;
@property (nonatomic, assign) BOOL ifFirst;
@property (nonatomic, assign) int page;
@end

@implementation CityViewController
- (void)dealloc
{
    [_cityId release];
    [_activityIndicatorView release];
    [_arrayOfScenery release];
    [_headView release];
    [_flowLayout release];
    [_collectionView release];
    [_city release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrayOfScenery = [NSMutableArray array];
        self.ifFirst = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // 创建进度轮
    self.activityIndicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50, 150, 100, 100)];
    self.activityIndicatorView.center = self.view.center;
    [self.activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.activityIndicatorView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.activityIndicatorView];
    [_activityIndicatorView release];
    [self getData];
}
- (void)createView
{
    // item
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.flowLayout.itemSize = CGSizeMake(120, 180);
    self.flowLayout.scrollDirection = 0;
    self.flowLayout.minimumInteritemSpacing = 20;
    self.flowLayout.minimumLineSpacing = 20;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(40, 20, 0, 20);
    self.flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 250);
    self.flowLayout.footerReferenceSize = CGSizeMake(20, 70);
    [_flowLayout release];
    
    // 创建瀑布流
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:self.flowLayout];
    [NightManager setBackgroundColorWithCollectionView:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    // 注册
    [self.collectionView registerClass:[AreaCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.collectionView registerClass:[AreaHeadReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
    [self addFooter];
}
- (void)addFooter
{
    // 添加上拉刷新尾部控件
    [self.collectionView addFooterWithCallback:^{
        [self add];
        
    }];
}
- (void)add{
    self.page++;
    self.ifFirst = NO;
    [self getData];
    [self.collectionView reloadData];
    [self.collectionView footerEndRefreshing];
    
}
- (void)getData
{
    if (self.ifFirst) {
        [self.activityIndicatorView startAnimating];
        [AFNetWorkingYU netWorkingHanderGetGataWithUrl:[NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/city_detail?app_installtime=1429487962&city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88307393003751&lon=121.5399246767973&page=1",self.cityId] resultBlock:^(id result) {
            NSDictionary *dic = result[@"data"];
            self.city = [[CityModel alloc] init];
            [self.city setValuesForKeysWithDictionary:dic];
            self.city.photos = [NSMutableArray array];
            for (NSString *url in dic[@"photos"]) {
                [self.city.photos addObject:[NSURL URLWithString:url]];
            }
            self.headView.photoArr = self.city.photos;
            [self.city release];
            
        }];
    }
    
    [AFNetWorkingYU netWorkingHanderGetGataWithUrl:[NSString stringWithFormat:@"http://open.qyer.com/qyer/onroad/poi_list?app_installtime=1429487962&category_id=32&city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&orderby=popular&page=%d",self.cityId,self.page] resultBlock:^(id result) {
        
        for (NSDictionary *dic in result[@"data"][@"entry"]) {
            SceneryModel *scenery = [[SceneryModel alloc]init];
            [scenery setValuesForKeysWithDictionary:dic];
            [self.arrayOfScenery addObject:scenery];
            [scenery release];
        }
        [self.activityIndicatorView stopAnimating];
        if (self.ifFirst) {
            [self createView];
        }
        
    }];

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrayOfScenery.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AreaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    SceneryModel *scenery = self.arrayOfScenery[indexPath.row];
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:scenery.photo] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    cell.nameLabel.text = [NSString stringWithFormat:@" %@\n %@",scenery.chinesename,scenery.englishname];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SceneryViewController *SceneryVC = [[SceneryViewController alloc]init];
    SceneryModel *scenery = self.arrayOfScenery[indexPath.row];
    SceneryVC.sceneryId = scenery.sceneryId;
    [self.navigationController pushViewController:SceneryVC animated:YES];
    [SceneryVC release];
}
- (void)push:(NSInteger)tag
{
    if (tag == 102) {
        RecommendTripViewController *recommendTripVC = [[RecommendTripViewController alloc]init];
        recommendTripVC.areaType = NO;
        recommendTripVC.areaId = self.cityId;
        [self.navigationController pushViewController:recommendTripVC animated:YES];
    }else{
        WebViewController *webVC = [[WebViewController alloc]init];
        webVC.url = self.city.overview_url;
        webVC.title = @"实用信息";
        [self.navigationController pushViewController:webVC animated:YES];
        [webVC release];
    }
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        self.headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"head" forIndexPath:indexPath];
        self.headView.delegate = self;
        self.headView.photoArr = self.city.photos;
        self.headView.nameLable.text = [NSString stringWithFormat:@"%@\n%@",self.city.chinesename,self.city.englishname];
        self.headView.ifHaveGuide = !self.city.has_guide;
        if (self.city.overview_url.length == 0) {
            self.headView.ifHaveInformation = YES;
        }
        self.headView.titleLabel.text = [NSString stringWithFormat:@"%@景点",self.city.chinesename];
        return self.headView;
    }else{
        UICollectionReusableView *foot = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"foot" forIndexPath:indexPath];
        return foot;
    }
}
- (void)setNightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithCollectionView:self.collectionView];
    [NightManager setBackgroundColorWithView:self.headView];
}
- (void)setLightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithCollectionView:self.collectionView];
    [NightManager setBackgroundColorWithView:self.headView];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
